package com.zxmr.webclient.upload;

import com.zxmr.domain.account.AccountType;
import com.zxmr.domain.account.User;
import com.zxmr.domain.file.File;
import com.zxmr.domain.file.ResourceAction;
import com.zxmr.domain.file.ResourceLevel;
import com.zxmr.domain.file.SystemFolder;
import com.zxmr.domain.service.ResourceService;
import com.zxmr.util.Utils;

public class SystemFolderUploader extends DefaultUploader{
	
	
	public File insertToDB(User user,String name,String url)throws UploadException
	{
		ResourceService service = new ResourceService();
		SystemFolder folder = service.querySystemFolder(user.getCompany(), this.folderType);
		
		if(folder == null)
			throw new UploadException("internal error");
		
		if(user.getAccountType()!= AccountType.Supervisor && 
				!service.isUserAuthorizedForAction(folder.getFolderId(), user, ResourceAction.addFile))
			throw new UploadException("you are not authorized to add file in this folder");
		
		
		File file = new com.zxmr.domain.file.File();
		
		file.setCompany(user.getCompany());
		file.setCreateTime(Utils.getCurrentTime());
		file.setOwner(user.getName());
		file.setName(name);
		file.setUrl(url);
		file.setLevel(ResourceLevel.Public);
		file.setParent(folder.getFolderId());
		
		long id = service.addFile(file);
		
		if(id <= 0)throw new UploadException("db insert error");
		file.setId(id);
		
		return file;
	}
	
}
